package org.mozhu.mboot.admin.personal;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.mozhu.mboot.admin.system.account.entity.User;
import org.mozhu.mboot.admin.system.account.service.UserService;
import org.mozhu.mboot.core.data.Searchable;
import org.mozhu.mboot.core.data.filter.Operator;
import org.mozhu.mboot.core.util.BeanUtils;
import org.mozhu.mboot.core.web.AjaxResponse;
import org.mozhu.mboot.core.web.bind.annotation.PageableDefaults;
import org.mozhu.mboot.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;

@RequestMapping("/personal")
@Controller
public class PersonalController extends BaseController {

	@Autowired
	UserService userService;

	@RequestMapping(value = "/contact")
	public String contact(HttpServletRequest request, Model model) {
		return viewName("contact");
	}

	@RequestMapping(value = "/contact/search", headers = "ajax=true", method = RequestMethod.POST)
	@ResponseBody
	@PageableDefaults(sort = "id=desc")
	public Object contactSearch(HttpServletRequest request, Searchable searchable) {
		searchable.addSearchFilter("deleted", Operator.EQ, Boolean.FALSE);
		Page<User> pageUser = userService.findAll(searchable);
		Page<Map<String, Object>> page = pageUser.map((user) -> createUser(user));
		return AjaxResponse.successWithData(page);
	}

	private Map<String, Object> createUser(User user) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", user.getName());
		map.put("enName", user.getEnName());
		map.put("email", user.getEmail());
		map.put("gender", user.getGender());
		return BeanUtils.transBean2Map(user, "name", "enName", "avatar", "email", "gender", "phone", "mobile", "fax", "birthday", "hireDate");
	}
}